package com.itguigu.rabbitmq.one;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Date 2021/7/18 9:59
 * @Created by wangle
 * @Description 消费者
 */
public class Consumer {

    //队列名称
    public static final String QUEUE_NAME = "hello";

    //接受消息
    public static void main(String[] args) throws IOException, TimeoutException {

        //创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();

        //创建工厂ip ，连接rabbitmq的对列
        factory.setHost("192.168.245.134");
        //用户名
        factory.setUsername("admin");
        //密码
        factory.setPassword("admin");
        //创建连接
        Connection connection = factory.newConnection();
        //创建信道
        Channel channel = connection.createChannel();


        //声明 接受消息
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            System.out.println("console打印" + new String(message.getBody()));

        };

        //取消消息时的回调
        CancelCallback cancelCallback = (consumerTag) -> {

            System.out.println("消息消费被中断");
        };

        /*
         * 消费者消息
         * 1.消费那个对列
         * 2.消费成功后是否要自动应答，true是自动应答，false是手动应答
         * 3.消费者未成功消费的回调
         * 4 .消费者取消消费的回调
         * */
        channel.basicConsume(QUEUE_NAME, true, deliverCallback, cancelCallback);


    }

}
